package com.example.shop.fgo.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shop.fgo.R;
import com.example.shop.fgo.entity.FileListEntity;
import com.example.shop.fgo.utils.IconUtil;
import com.example.shop.fgo.utils.SizeUtil;
import com.example.shop.fgo.utils.TimeUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

public class ItemLvFileAdapter extends BaseAdapter implements View.OnClickListener {

    private final DisplayImageOptions mOptions;
    private List<FileListEntity.DataEntity> mEntities;
    public final List<FileListEntity.DataEntity> mDataEntitiesChecked =new ArrayList<>();
    private Context mContext;
    private LayoutInflater layoutInflater;
    public static final int TYPE_DIR = 0;
    public static final int TYPE_FILE = 1;

    public ItemLvFileAdapter(Context context,List<FileListEntity.DataEntity> objects) {
        this.mContext = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.mEntities =objects;


        mOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.file_more)
                .showImageForEmptyUri(R.drawable.file_more)
                .showImageOnFail(R.drawable.file_more)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(false)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(7))//圆角的padding？
                .build();
    }
    public void setEntities(List<FileListEntity.DataEntity> entities) {
        mEntities = entities;
        //重置之后选择的文件清空
        mDataEntitiesChecked.clear();
        this.notifyDataSetChanged();
    }
    public void addDatas(List<FileListEntity.DataEntity> dataEntities){
        //addAll把一个集合里的东西加到另一个里面
        mEntities.addAll(dataEntities);

        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mEntities.size();
    }

    @Override
    public FileListEntity.DataEntity getItem(int position) {
        return mEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType=getItemViewType(position);
        ViewHolder viewHolder=null;
        if (convertView == null) {
            if (itemViewType==TYPE_DIR) {
                convertView=layoutInflater.inflate(R.layout.item_lv_dir,parent,false);
            }else{
                convertView=layoutInflater.inflate(R.layout.item_lv_file,parent,false);
            }
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        FileListEntity.DataEntity dataEntity=this.getItem(position);
        viewHolder.mTvTitle.setText(dataEntity.getN());

        viewHolder.mCheckBox.setTag(dataEntity);
        viewHolder.mCheckBox.setOnClickListener(this);
        viewHolder.mCheckBox.setChecked(dataEntity.isChecked());
        if (itemViewType==TYPE_FILE) {
            viewHolder.mTvSize.setText(SizeUtil.formatSize(dataEntity.getS()));
            viewHolder.mTvTime.setText(TimeUtil.formatTimeInS(dataEntity.getT()));

            if (TextUtils.isEmpty(dataEntity.getU())) {
                //加载本地图标
                int icon = IconUtil.getDrawable(dataEntity.getIco());
                ImageLoader.getInstance().displayImage("drawable://"+icon,viewHolder.mImageViewIcon,mOptions);
            }else{
                //加载缩略图
                ImageLoader.getInstance().displayImage(dataEntity.getU(),viewHolder.mImageViewIcon,mOptions);
            }
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
       FileListEntity.DataEntity dataEntity=this.getItem(position);
        if (TextUtils.isEmpty(dataEntity.getCid())) {
            //文件
            return TYPE_FILE;
        }else {
            //文件夹
            return TYPE_DIR;
        }
    }


    @Override
    public void onClick(View v) {
        CheckBox checkBox= (CheckBox) v;
        FileListEntity.DataEntity dataEntity= (FileListEntity.DataEntity) v.getTag();
        //把CheckBox的选中状态  存到dataentity中
        dataEntity.setChecked(checkBox.isChecked());
        if (checkBox.isChecked()) {
            mDataEntitiesChecked.add(dataEntity);
        }else{
            mDataEntitiesChecked.remove(dataEntity);
        }
    }

    protected class ViewHolder {
        private TextView mTvTitle;
        private CheckBox mCheckBox;

        private ImageView mImageViewIcon;
        private TextView mTvSize;
        private TextView mTvTime;

        public ViewHolder(View itemView) {
            mTvTitle= (TextView) itemView.findViewById(R.id.tv_title_item);
            mCheckBox= (CheckBox) itemView.findViewById(R.id.cb_item);

            mImageViewIcon = (ImageView) itemView.findViewById(R.id.imgv_icon_item_file);
            mTvSize = (TextView) itemView.findViewById(R.id.tv_size_item_file);
            mTvTime = (TextView) itemView.findViewById(R.id.tv_time_item_file);
        }
    }
}
